<?php


namespace app\oa\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class Message extends BaseController
{
protected $table='message';

    public function index()
    {
        $param = get_params();
        $isDbs=$param['isDbs']??0;
        if (request()->isAjax()) {
            $status = isset($param['status'])?$param['status']:0;
            $user_id = $this->uid;
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $where[]=['m.action_id','>',0];
            $where[]=['m.status','=',1];
            if ($isDbs==2){
                $where[]=['m.action_name','<>','flow_check'];
            }else{
                $where[]=['m.action_name','=','flow_check'];
            }

            $sql=Db::name($this->table)
                ->alias('m')
                ->leftJoin('approve a','a.id=m.action_id')
                ->leftJoin('admin ad','ad.id=m.createId');

            $haveAuth=false;
            if ($this->isHaveAuth('see_all')){
                $haveAuth=true;
            }elseif($this->isHaveAuth('see_depart')){
                $haveAuth=true;
                $where[]=['a.department_id','in',$this->dSons];
            }elseif($this->isHaveAuth('see_self')){
                $haveAuth=true;
                $where[]=['to_uid','=',$this->uid];
            }
            if (!$haveAuth){
                return table_assign(1,'暂无查看权限');
            }
            if (isset($param['status'])&&$param['status']){
                if ($param['status']==1){
                    $where[]=['m.read_time','=',0];
                }else{
                    $where[]=['m.read_time','>',0];
                }
            }
            if (isset($param['uid'])&&$param['uid']){
                $where[]=['ad.id','=',$param['uid']];
            }
//            if (isset($param['check_status'])&&$param['check_status']){
//                $where[]=['a.check_status','=',$param['check_status']];
//            }

            $list=$sql
                ->field('m.id,a.id as aId,m.title as msTitle,m.read_time,ad.name as createUser,a.check_status,(select title from fy_flow_type ft where ft.id=flow_id)spTitle')
                ->where($where)
                ->order('m.id DESC')
                ->paginate($rows)->each(function ($ml){
                    $tileStr=$ml['msTitle'];
                    $tileArr=explode('提交',$tileStr);
                    if (!$ml['createUser']){
                        $ml['createUser']='系统';
                    }
                    $stt1=str_replace(['『','』'],'#-#',$tileStr);
                    $typeArr=explode('#-#',$stt1);
                    $statusArr=explode('已被',$tileStr);
                    $ml['status']=$statusArr[1]??'';
                    $ml['spTitle']=$typeArr[1]??'';
                    if (!$ml['status']&&!$ml['check_status']){
                        $ml['check_status']=0;
                    }elseif ($ml['status']&&!$ml['check_status']){
                        $ml['check_status']=2;
                    }
                    return $ml;
                });
            return table_assign(0, '', $list);
        } else {
            $type = Db::name('FlowType')->whereOr('status',1)->select()->toArray();
            View::assign('type', $type);
            View::assign('isDbs', $isDbs);
            return view();
        }
    }

    public function readed()
    {
        $id=$this->param['id']??'';
        if (!$id){
            return to_assign(1,'参数缺失');
        }
        $ids=explode(',',$id);
        try {
            Db::name($this->table)->where('id','in',$ids)->update([
                'read_time'=>time()
            ]);
            add_log('view', $this->param['id'], $this->param);
        }catch (\Exception $e){
            return to_assign(1,'操作失败:'.$e->getMessage());
        }
        return to_assign();
    }

    public function info()
    {

        $id=$this->param['id'];
        $stats=['待审批','审批中','已通过','已拒绝','已撤销'];
        $info=Db::name($this->table)
            ->where('id',$id)
            ->find();
        $tileStr=$info['title'];
        $tileArr=explode('提交了',$tileStr);
        $info['createUser']=$tileArr[0]??'';
        $stt1=str_replace(['『','』'],'#-#',$tileStr);
        $typeArr=explode('#-#',$stt1);
        $statusArr=explode('已被',$tileStr);
        $info['statusText']=$statusArr[1]??'';
        $info['spTitle']=$typeArr[1]??'';
        view::assign('info',$info);
        return view();
    }
}